package com.eascs.applyLoan.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.app.dao.PageResult;

import com.eascs.common.util.UrlUtil;
import com.eascs.applyLoan.dao.O2oLoanInfoDao;
import com.eascs.applyLoan.entity.O2oLoanEntity;
import com.eascs.applyLoan.entity.O2oLoanInforEntity;
import com.eascs.applyLoan.service.O2oLoanInfoService;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.PaginationRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;
import com.eascs.site.view.button.ShowButtonFilter;

import com.eascs.web.sso.UserHolder;
import com.eascs.workflow.common.service.sys.SysOrgService;
import com.eascs.workflow.common.service.wf.WorkFlowService;


@RestController
@RequestMapping("/restLoan")
public class O2oLoanInfoListRestController {
	@Autowired
	O2oLoanInfoService o2oLoanInfoService;
	@Autowired
	O2oLoanInfoDao o2oLoanInfoDao;
	@Reference
	private WorkFlowService workFlowService;
	@Reference
	private SysOrgService sysOrgService;
	@RequestMapping("/rApply")
	public PaginationRestResult O2oLoanInfo(O2oLoanInforEntity oe) {
		oe.setPageSize(10);

		// 若startDate 与 endDate相同，则默认为查询当天所有贷款信息
		String pageUrl = "/restLoan/rApply";
		// 分页url
		pageUrl = UrlUtil.appendUrl(pageUrl, "province", oe.getProvince() == null ? "" : oe.getProvince());
		pageUrl = UrlUtil.appendUrl(pageUrl, "city", oe.getCity() == null ? "" : oe.getCity());
		pageUrl = UrlUtil.appendUrl(pageUrl, "startDate", oe.getStartDate() == null ? "" : oe.getStartDate());
		pageUrl = UrlUtil.appendUrl(pageUrl, "endDate", oe.getEndDate() == null ? "" : oe.getEndDate());
		pageUrl = pageUrl + "&pageIndex=";
		// 获取数据库数据
		PageResult<Object[]> pageResult = o2oLoanInfoService.findByPage(oe);
		// 设置表头
		String titles[] = { "申请时间", "姓名", "身份证号", "手机", "申请金额", "期限", "贷款类型", "门店名称", "门店注册时间", "省份", "城市", "详细地址",
				"合作平台", "合作时间", "省区负责人", "信贷经理" };
		// 生成分页对象
		String update = "/restLoan/view_up?id=";
		String user = UserHolder.getUser().getCode();
		Button assignManager = ButtonHelper.createAjaxButton("分配信贷经理 ", update , "allotManager");
		assignManager.setShowButtonFilter(new ShowButtonFilter() {
			@Override
			public boolean filter(Object param) {
				
				Object[] params = (Object[]) param;
				if (user.equals(params[17]+"")||UserHolder.getUser().isSuperAdministrator()) 
					return true;
				else
					return false;
			}
		});
		Button[] titleButtons = new Button[] { assignManager };
		PaginationInfo paginationInfo = new PaginationInfo(pageResult, titles, pageUrl);
		paginationInfo.setMoreButtons(titleButtons);
		paginationInfo.setBuildPagingNavigation(true);
		paginationInfo.setFirstRenderColumnIndex(1);
		paginationInfo.setRendTo("#o2o_result");
		ViewContext.put("vo", oe);
		ViewContext.put("paginationInfo", paginationInfo);
		return PaginationRestResult.buildPaginationRestResult(paginationInfo);
	}

	@RequestMapping("/view_up")
	public BaseRestResult<String> viewUpdate(O2oLoanInforEntity oe) {
		ViewContext.put("id", oe.getId());
		return HtmlRestResult.popHtmlFragment("/applyLoan/update_manager");
	}

	@RequestMapping("/assignManager")
	public BaseRestResult<String> assignManager(O2oLoanInforEntity oe, O2oLoanEntity e)

	{

		e.setId(oe.getId());
		e.setCredit_manager(oe.getCreditManager());
		if (e.getCredit_manager() == null) {
			return BaseRestResult.warn("请填写要分配的信贷经理");

		} else {
			o2oLoanInfoDao.updateById(e);
			return BaseRestResult.info("信贷经理分配成功！").andRefresh();
		}
	}

  	
    
}
